package com.shop.myweb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shop.myweb.common.BaseContext;
import com.shop.myweb.common.R;
import com.shop.myweb.entity.Address;
import com.shop.myweb.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/address")
@Transactional
public class addressController {

    @Autowired
    private AddressService addressService;


    /**
     * 新增
     */
    @PostMapping
    public R<Address> save(@RequestBody Address address) {
        log.info("address:{}", address);
        addressService.save(address);
        return R.success(address);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    public R<Address> setDefault(@RequestBody Address address) {
        log.info("address:{}", address);
        LambdaUpdateWrapper<Address> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Address::getUserId, BaseContext.getCurrentId());
        wrapper.set(Address::getIsDefault, 0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressService.update(wrapper);

        address.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressService.updateById(address);
        return R.success(address);
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUserId, id);
        Address address = addressService.getOne(queryWrapper);
        if (address != null) {
            return R.success(address.getDetail());
        } else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    public R<Address> getDefault(Long userId) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUserId, userId);
        queryWrapper.eq(Address::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        Address address = addressService.getOne(queryWrapper);

        if (null == address) {
            return R.error("没有找到该对象");
        } else {
            return R.success(address);
        }
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list/{id}")
    public R<ArrayList<String>> list(@PathVariable Long id) {

        //条件构造器
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != id, Address::getUserId, id);

        List<Address> addressList = addressService.list(queryWrapper);
        ArrayList<String> stringList = new ArrayList<>();
        for (Address address : addressList) {
            if (address.getDetail() != null)
                stringList.add(address.getDetail());
        }

        //SQL:select * from address_book where user_id = ? order by update_time desc
        return R.success(stringList);
    }
}
